<template>
  <el-card class="box-card margin-200" v-loading="loading" element-loading-text="拼命加载中">
    <div slot="header" class="clearfix">
      <el-row>
        <el-col :md="6">
          <el-input placeholder="检索：角色组名、描述" icon="search" v-model="keys">
          </el-input>
        </el-col>
        <el-col :md="18" class="text-right">
          <el-button type="primary" @click="handleAdd('')" icon="plus">
            添加角色
          </el-button>
        </el-col>
      </el-row>
    </div>
    <div v-bind:style="{ 'min-height': _PageHeight+52+'px'}">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" width="55">
        </el-table-column>
        <el-table-column prop="Account" label="角色组名称" width="150">
        </el-table-column>
        <el-table-column prop="describe" label="角色描述">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" :filters="[{ text: '正常', value: 1 }, { text: '禁用', value: 0 }]" :filter-method="filterTag">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'" close-transition>
              <span v-if="scope.row.status==0">禁用</span>
              <span v-if="scope.row.status==1">正常</span>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="360">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" v-if="scope.row.status==1" class="height-300" type='warning' @click="handleStop(scope.row,0)">
                <i class="ti-lock mini"></i> 禁用
              </el-button>
              <el-button size="small" v-if="scope.row.status==0" class="height-300" type='info' @click="handleStop(scope.row,1)">
                <i class="ti-unlock mini"></i> 启用
              </el-button>
              <el-button size="small" class="height-300" type='success' @click="handleAuth(scope.row._id)">
                <i class="ti-settings"></i> 权限管理
              </el-button>
              <el-button size="small" class="height-300" type='primary' @click="handleAdd(scope.row)">
                <i class="ti-pencil-alt"></i> 编辑
              </el-button>
              <el-button size="small" class="height-300" type='danger' @click="handleDelelt(scope.row._id)">
                <i class="el-icon-delete2"></i> 删除
              </el-button>
              <el-button size="small" class="height-300" type="info" @click="handleAdmin(scope.row._id)">成员管理</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--添加编辑角色弹框-->
    <el-dialog title="角色组信息" v-model="SearchMoreForm" @close="handleClose">
      <el-form label-suffix="：" :model="formData" :rules="rules" ref="formData" :label-width="formLabelWidth">
        <el-form-item label="角色组名称" prop="Account">
          <el-input v-model="formData.Account" placeholder="请填写角色组名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formData.describe" type="textarea" :rows="2" placeholder="请填写角色描述"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="false" @click="handleKeep('formData')">确认保存</el-button>
      </div>
    </el-dialog>
    <Quanxian :dialogStatus="dialog" :qxid="qxid" @CB-dialogStatus="CB_dialogStatus"></Quanxian>
  </el-card>
</template>
<script>
import Quanxian from '@/page/system/rules_quanxian'

export default {
  activated: function() {
    this.getData()
  },
  data() {
    return {
      keys: '',
      loading: false,
      tableData: [],
      value: '',
      SearchMoreForm: false,
      formData: {
        Account: '',
        describe: '',
        status: 1
      },
      formLabelWidth: '120px',
      rules: {
        Account: [{
          required: true,
          message: '请填写角色组名称',
          trigger: 'blur'
        }]
      },
      url: '',
      dialog: false,
      qxid: '',
      status: 1,
    }
  },
  watch: {
    // 'keys': function(newValue, oldValue) {
    //   if (newValue == oldValue) return;
    //   this.keys = newValue;
    //   this.getData();
    // }
  },
  methods: {
    //跳成员管理页面
    handleAdmin(id) {
      this.group._id = id
      this.$router.push({ name: 'users' })
    },
    //删除管理员
    handleDelelt(val) {
      this.$confirm('删除后不可恢复，确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.fetch('/userGroup/deletes', {
          _id: val
        }, this.$store.getters.Request_Head, 'POST').then((userData) => {
          if (userData.status.code !== 1001) {
            this.$message.error(userData.data ? userData.data : userData.status.title)
          } else {
            this.getData()
          }
        }).catch(() => {
          this.$emit('API-ERR');
        })
      }).catch(() => {

      });
    },
    //禁用，启用
    handleStop(row, val) {
      this.url = '/userGroup/update'
      this.formData = row
      this.formData.status = val;
      this.setData();
    },
    //权限管理
    handleAuth(id) {
      this.qxid = id
      this.dialog = true
    },
    //添加编辑角色
    handleAdd(row) {
      this.SearchMoreForm = true
      if (row == "") {
        this.url = '/userGroup/insert'
      } else {
        this.url = '/userGroup/update'
        this.formData = row
      }
    },
    //保存数据
    handleKeep(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.setData();
          this.handleClose()
        } else {
          return false;
        }
      })
    },
    //编辑保存数据
    setData() {
      this.fetch(this.url, {
        url: this.url,
        data: this.formData
      }, this.$store.getters.Request_Head, 'POST').then((userData) => {
        if (userData.status.code !== 1001) {
          this.$message.error(userData.data ? userData.data : userData.status.title)
        } else {
          this.$message({
            message: '数据保存成功！',
            type: 'success'
          });
          this.getData()
        }
      }).catch(() => {
        this.$emit('API-ERR');
      })
    },
    //筛选过滤
    filterTag(value, row) {
      return row.status === value;
    },
    //获取数据
    getData() {
      this.loading = true
      this.fetch('/userGroup/index', {}, this.$store.getters.Request_Head, 'POST').then((userData) => {
        if (userData.status.code !== 1001) {
          this.$message.error(userData.data ? userData.data : userData.status.title)
        } else {
          this.loading = false
          this.tableData = userData.data
          this.$store.commit('setRulesArr', this.tableData)
        }
      }).catch(() => {
        this.loading = false
        this.$emit('API-ERR');
      })
    },
    //关闭弹框
    handleClose() {
      this.$refs['formData'].resetFields();
      this.SearchMoreForm = false
      this.formData = {
        title: '',
        describe: '',
        status: 1
      }
    },
    CB_dialogStatus(val) {
      if (val == 1) {
        this.getData()
      }
      this.qxid = ''
      this.dialog = false
    }
  },
  components: {
    Quanxian
  }
}

</script>
<style>
.height-300 {
  height: 30px;
}

</style>
